package com.ereport.controller.accountsController;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ereport.dao.AccountsDAO;
import com.ereport.entity.AccContacts;
import com.ereport.entity.Accounts;
import com.ereport.entity.Executives;
import com.ereport.entity.Expenses;

@Controller
@RequestMapping("/accounts")
public class AccountController {

	@RequestMapping("/open.do")
	public ModelAndView open(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		AccountsDAO adao=new AccountsDAO();
		Set<String> daset=new HashSet<String>();
		StringBuilder sb=new StringBuilder();
		List<Accounts> acList=null;
		try {
			acList=adao.getAccounts(ex.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(acList!=null){
			for(int i=0;i<acList.size();i++){
				String org=acList.get(i).getAccName();
				daset.add(org);
			}
			
			Iterator itr=daset.iterator();
			while(itr.hasNext()){
				Object obj=itr.next();
				String or=(String)obj;
				if(or.length()!=0){
				sb.append(or+"/");	
				}
						
			}
			}
		
		model.setViewName("/jsp/accContacts.jsp");
		model.addObject("acList",sb.toString());
		return model;
	}
	@RequestMapping(value="/save.do", method=RequestMethod.POST)
	public ModelAndView insert(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		String org=req.getParameter("org");
		String acNum=req.getParameter("acNum");
		String indsType=req.getParameter("indsType");
		String prAccOwnName=req.getParameter("prAccOwnName");
		String phn=req.getParameter("phn");
		String fax=req.getParameter("fax");
		String email=req.getParameter("email");
		String website=req.getParameter("website");
		String shpAddr=req.getParameter("shpAddr");
		String invAddr=req.getParameter("invAddr");
		String taxRegdNum=req.getParameter("taxRegdNum");
		String accCreatedDate=req.getParameter("accCreatedDate");
		DateFormat df=new SimpleDateFormat("dd-MM-yyyy");
		String rCount=req.getParameter("rCount");
		int rc=Integer.parseInt(rCount);
		
		Date d=null;
		try {
			d = df.parse(accCreatedDate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		d=new java.sql.Date(d.getTime());
		AccountsDAO adao=new AccountsDAO();
		Accounts ac=null;
		try {
			 ac=adao.getAcc(ex.getId(), org);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int acid=0;
		if(ac!=null){
			acid=ac.getId();
			Accounts acc=null;
			try {
				acc=new Accounts(acid,ex,org,acNum,indsType,prAccOwnName,phn,fax,email,website,shpAddr,invAddr,taxRegdNum,d);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Set<AccContacts> accContSet=new HashSet<AccContacts>();
			for(int r=1;r<=rc;r++){
				String contName=req.getParameter("contName"+r);
				String contPin=req.getParameter("contPin"+r);
				String contEmail=req.getParameter("contEmail"+r);
				String contPhone=req.getParameter("contPhone"+r);
				String contJobRole=req.getParameter("contJobRole"+r);
				String lstCont=req.getParameter("lstCont"+r);
				Date dt=null;
				if(!lstCont.equals("")){
				try {
					dt = df.parse(lstCont);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dt=new java.sql.Date(dt.getTime());
				}
				AccContacts acCont=null;
				try {
					acCont=new AccContacts(contName,contPin,contEmail,contPhone,contJobRole,dt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				accContSet.add(acCont);
			}
			acc.setAccContact(accContSet);
			
			try {
				adao.insert(acc);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else {
		
			Accounts acc=null;
			try {
				acc=new Accounts(ex,org,acNum,indsType,prAccOwnName,phn,fax,email,website,shpAddr,invAddr,taxRegdNum,d);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Set<AccContacts> accContSet=new HashSet<AccContacts>();
			for(int r=1;r<=rc;r++){
				String contName=req.getParameter("contName"+r);
				String contPin=req.getParameter("contPin"+r);
				String contEmail=req.getParameter("contEmail"+r);
				String contPhone=req.getParameter("contPhone"+r);
				String contJobRole=req.getParameter("contJobRole"+r);
				String lstCont=req.getParameter("lstCont"+r);
				Date dt=null;
				if(!lstCont.equals("")){
				try {
					dt = df.parse(lstCont);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				dt=new java.sql.Date(dt.getTime());
				}
				AccContacts acCont=null;
				try {
					acCont=new AccContacts(contName,contPin,contEmail,contPhone,contJobRole,dt);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				accContSet.add(acCont);
			}
			acc.setAccContact(accContSet);
			
			try {
				adao.insert(acc);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		}
		
		
		
		model.setViewName("redirect:open.do");
		return model;
	}
	@RequestMapping(value="/getAcc.do")
	public ModelAndView getAcc(HttpServletRequest req,HttpSession session,ModelAndView model){
		Executives ex=(Executives)session.getAttribute("loginUser");
		int exeid=ex.getId();
		String org=req.getParameter("orgn");
		System.out.println("org "+org);
		if(!org.equals("")){
			if(req.getParameter("exe")!=null){
				String exid=req.getParameter("exe");
				exeid=Integer.parseInt(exid);
			}
		AccountsDAO acDao=new AccountsDAO();
		Accounts acc=null;
		try { 
			acc=acDao.getAcc(exeid,org);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(acc!=null){
		int acId=acc.getId();
		List<AccContacts> acContList=null;
		try {
			acContList=acDao.getCont(acId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/editAccount.jsp");
		model.addObject("acc",acc);
		model.addObject("acContList",acContList);
		
		return model;
		}
	}
		
	model.setViewName("redirect: open.do");
	return model;
}
	@RequestMapping(value="/getClientDet.do",method = RequestMethod.GET)
	public @ResponseBody
	String getClientDet(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			
			return "/jsp/login.jsp";
		}
		AccountsDAO acDao=new AccountsDAO();
		StringBuilder sb=new StringBuilder();
		List<Accounts> c=acDao.getAllCli(ex.getId());
		Iterator itr=c.iterator();
		while(itr.hasNext()==true){
			Object ob=itr.next();
			Accounts cli=(Accounts)ob;
			sb.append(cli.getAccName());
			sb.append(",");
		}
		return sb.toString();
	}
	@RequestMapping(value="/clientEdit.do",method = RequestMethod.POST)
	public  ModelAndView clientEdit(HttpServletRequest req,HttpSession session, ModelAndView model) throws Exception{
		String org=req.getParameter("clientName");
		Executives ex=(Executives)session.getAttribute("loginUser");
		if(ex==null){
			model.setViewName("/jsp/login.jsp");
			return model;
		}
		AccountsDAO acDao=new AccountsDAO();
		Accounts acc=null;
		try { 
			acc=acDao.getAcc(ex.getId(),org);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(acc!=null){
		int acId=acc.getId();
		List<AccContacts> acContList=null;
		try {
			acContList=acDao.getCont(acId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.setViewName("/jsp/editAccount.jsp");
		model.addObject("acc",acc);
		model.addObject("acContList",acContList);
		
		return model;
		}
		return model;

	}
}